// ---------------------------------------------------------------------
//
// -- 世界排行面板
// -- author £
//
// ---------------------------------------------------------------------
const CommonUtil = require('CommonUtil'),
    superClass = require('AlertWindow');

cc.Class({
    extends: superClass,

    properties: {
        listContent: cc.Node,
        worldP: cc.Prefab,
        rankPanelBg: cc.Node,
        lbGap: cc.Label,
        tipGap: cc.Node,
        tipRank: cc.Node,
        lbRank: cc.Label,
        tipNoPlay: cc.Node,
        btnPre: cc.Button,
        btnNext: cc.Button,
        btnReturn: cc.Node,
        btnHistory: cc.Node,
    },

    ctor: function () {
        this._worldPool = new cc.NodePool();
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        CommonUtil.mysuper(superClass, this, 'onLoad');
        this.rankPanelBg.height += require('UIManager').getRankBgDH();

        let i, len = require('UIManager').getRankItemCount();

        for (i = 0; i < len; ++i) {
            this._worldPool.put(cc.instantiate(this.worldP));
        }
    },

    start() {
    },

    show: function () {
        CommonUtil.mysuper(superClass, this, 'show');
        CommonUtil.resetPool(this._worldPool, this.listContent);
        this.tipGap.active = false;
        this.tipRank.active = false;
        this.tipNoPlay.active = false;
    },

    setData: function (data) {
        let list = data.list,
            len = list.length,
            i, item, itemC;

        for (i = 0; i < len; ++i) {
            if (!this._worldPool.size()) break;

            item = this._worldPool.get();
            this.listContent.addChild(item);
            itemC = item.getComponent(require('WorldRankItem'));
            itemC.setData(list[i]);
        }


        this.tipRank.active = data.rank >= 1 && data.rank <= 100;
        this.tipGap.active = !this.tipRank.active && data.gap > 0;
        this.tipNoPlay.active = !this.tipRank.active && data.gap <= 0;
        this.lbGap.string = data.gap;
        this.lbRank.string = data.rank;
    },

    setListData: function (list) {
        let len = list.length,
            i, item, itemC;

        CommonUtil.resetPool(this._worldPool, this.listContent);

        for (i = 0; i < len; ++i) {
            if (!this._worldPool.size()) break;

            item = this._worldPool.get();
            this.listContent.addChild(item);
            itemC = item.getComponent(require('WorldRankItem'));
            itemC.setData(list[i]);
        }
    },

    clear: function () {
        require('CommonUtil').resetPool(this._worldPool, this.listContent);
    },

    // update (dt) {},
});
